package com.pw.study.flow

object For {
  def main(args: Array[String]): Unit = {
    for7()
  }

  /**
   * 循环返回值
   */
  def for7(): Unit = {
    val res = for (i <- 1 to 10) yield i
    println(res)

  }

  /**
   * 循环变量
   */
  def for6(): Unit = {
    for (i <- 1 to 10; j = 10 - i) {
      println(s"i=${i} j=${j}")
    }

  }

  /**
   * 循环嵌套
   */
  def for5(): Unit = {
    println("嵌套循环。。。")
    for (i <- 1 to 5; j <- 1 until (5)) {
      print(s"i=${i} ")
      print(s"j=${j} ")
    }
    println()
    for (i <- 1 to 5) {
      for (j <- 1 to 5) {
        print(s"i=${i} j=${j} ")
      }
      println()
    }

  }

  /**
   * 循环步长
   */
  def for4(): Unit = {
    println("步长学习")
    for (i <- 1 to 20 by 2) {
      println(s"${i} ")
    }

  }

  /**
   * 循环守卫
   */
  def for3(): Unit = {
    println(s"循环数据")
    for (i <- 1 to 10 if i % 2 == 0) {
      print(s"${i} ")
    }
  }

  /**
   * 循环until
   */
  def for2(): Unit = {
    println(s"循环数据")
    for (i <- 1 until (10)) {
      print(s"${i} ")
    }
    println(s"循环数据")
    for (i <- 1 until 10) {
      print(s"${i} ")
    }

  }

  /**
   * 循环to
   */
  def for1(): Unit = {
    println(s"循环数")
    for (i <- 1 to 10) {
      print(s"${i} ")
    }
  }

}
